package com.ryskoo.customer.vendordiscounts.repository;

import com.ryskoo.catering.constant.HCLCacheConstant;
import com.ryskoo.customer.vendordiscounts.domain.VendorDiscountsPO;
import com.ryskoo.framework.core.repository.BasicRedisImpl;
import com.ryskoo.framework.core.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Repository;

import javax.annotation.PostConstruct;


@Primary
@Repository
public class VendorDiscountsRedisImpl extends BasicRedisImpl<VendorDiscountsPO> implements IVendorDiscountsRepository {

	private static final String cacheKey = HCLCacheConstant.HCL_VENDORDISCOUNTS_KEY;

	@Autowired
	private RedisUtil redisClient;

	@Autowired
	private IVendorDiscountsRepository vendorDiscountsRepository;

	@PostConstruct
	public void init() {
		this.setRedisClient(redisClient);
		this.setDao(vendorDiscountsRepository);
	}

	public VendorDiscountsRedisImpl() {
		super(cacheKey);
	}

	@Override
	public void create(VendorDiscountsPO vendorDiscountsPO) {
		super.create(vendorDiscountsPO);
		redisClient.setObject(cacheKey + vendorDiscountsPO.getVendorUuid(), vendorDiscountsPO);
	}

	@Override
	public void update(VendorDiscountsPO vendorDiscountsPO) {
		super.update(vendorDiscountsPO);
		redisClient.setObject(cacheKey + vendorDiscountsPO.getVendorUuid(), vendorDiscountsPO);
	}

	@Override
	public VendorDiscountsPO getByVendorUuid(String vendorUuid) {
		VendorDiscountsPO po = (VendorDiscountsPO) redisClient.getObject(cacheKey + vendorUuid, VendorDiscountsPO.class);
		if(null != po){
			return po;
		}
		po = vendorDiscountsRepository.getByVendorUuid(vendorUuid);
		if(null != po){
			redisClient.setObject(cacheKey + vendorUuid, po);
		}
		return po;
	}
}
